package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.framework.fund.widget.GestureContentView;
import com.ciban.smarthome.common.framework.fund.widget.LockIndicator;
import com.ciban.smarthome.common.framework.fund.widget.GestureDrawline.GestureCallBack;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.ui.base.BaseActivity;

public class PwdFundActivity extends BaseActivity implements OnClickListener {
	private FrameLayout fl_content;
	private TextView tv_tip;
	private LockIndicator lock_show;

	private String pwd = "";
	private boolean isResetPwd = false;
	private GestureContentView mGestureContentView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_pwd_fund);

		initData();

		initViews();
	}

	private void initData() {
		if (getIntent().hasExtra("pwd")) {
			pwd = getIntent().getStringExtra("pwd");
		}
		if (getIntent().hasExtra("reset")) {
			isResetPwd = getIntent().getExtras().getBoolean("reset");
		}
	}

	private void initViews() {
		((TextView) findViewById(R.id.title)).setText("设置手势");
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		((ImageView) findViewById(R.id.title_add)).setVisibility(View.GONE);

		fl_content = (FrameLayout) findViewById(R.id.pwd_fund_content);
		tv_tip = (TextView) findViewById(R.id.pwd_fund_tip);
		lock_show = (LockIndicator) findViewById(R.id.pwd_fund_lock_indicator);

		if (StringUtils.isEmpty(pwd)) {
			showFundView();
		} else {
			showFundView(pwd);
		}

	}

	@Override
	public void onClick(View v) {

	}

	private void showFundView() {
		fl_content.setVisibility(View.VISIBLE);

		tv_tip.setText("第一次输入密码");

		mGestureContentView = new GestureContentView(this, false, null,
				new GestureCallBack() {

					@Override
					public void onGestureCodeInput(String inputCode) {
						if (StringUtils.isEmpty(inputCode)
								|| inputCode.length() < 4) {
							displayShort("输入的位数太短");
							return;
						}
						lock_show.setPath(inputCode);
						if (StringUtils.isEmpty(pwd)) {
							pwd = inputCode;
							mGestureContentView.clearDrawlineState(1300L);
							tv_tip.setText("第二次输入密码");
						} else {
							if (pwd.equals(inputCode)) {
								Intent intent = new Intent();
								intent.putExtra("pwd", inputCode);
								setResult(RESULT_OK, intent);
								finish();
							} else {
								mGestureContentView.clearDrawlineState(1300L);
								tv_tip.setText("两次输入不一致，请重新输入");
							}
						}
					}

					@Override
					public void checkedSuccess() {
					}

					@Override
					public void checkedFail() {
					}
				});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(fl_content);
	}

	private void showFundView(String pwd) {
		fl_content.setVisibility(View.VISIBLE);

		// lock_show.setPath(pwd);

		tv_tip.setText("请输入正确的密码");

		mGestureContentView = new GestureContentView(this, true, pwd,
				new GestureCallBack() {

					@Override
					public void onGestureCodeInput(String inputCode) {

					}

					@Override
					public void checkedSuccess() {
						mGestureContentView.clearDrawlineState(0L);
						if (isResetPwd) {
							showFundView();
							PwdFundActivity.this.pwd = "";
						} else {
							setResult(RESULT_OK);
							finish();
						}
					}

					@Override
					public void checkedFail() {
						mGestureContentView.clearDrawlineState(1300L);
						Toast.makeText(PwdFundActivity.this, "密码错误", 1000)
								.show();
					}
				});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(fl_content);
	}
}
